package com.hehe.boot.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hehe.boot.mapper.DiagnosticMapper;
import com.hehe.boot.pojo.*;
import com.hehe.boot.pojo.DiagnosticHttp.*;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DiagnosticService extends ServiceImpl<DiagnosticMapper, Diagnostic> {

    private final DiagnosticDrugService diagnosticDrugService;
    private final DrugService drugService;
    private final DoctorService doctorService;
    private final UserService userService;
    private final ReservationService reservationService;

    public DiagnosticService(DiagnosticDrugService diagnosticDrugService,DrugService drugService,DoctorService doctorService,UserService userService,ReservationService reservationService){
        this.diagnosticDrugService = diagnosticDrugService;
        this.drugService = drugService;
        this.doctorService = doctorService;
        this.userService = userService;
        this.reservationService = reservationService;
    }

    public Long userAdd(Long reservationId,Long doctorId, Long userId, String trouble) {
        Diagnostic diagnostic = new Diagnostic();
        diagnostic.setUserId(userId);
        diagnostic.setTrouble(trouble);
        diagnostic.setDoctorId(doctorId);
        Boolean flag = this.save(diagnostic);
        if (flag == false){
            return Long.valueOf(-1);
        }

        UpdateWrapper<Reservation> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",reservationId);
        updateWrapper.set("diagnostic_id",diagnostic.getId());
        this.reservationService.update(updateWrapper);
        return diagnostic.getId();
    }

    @Transactional
    public Long doctorAdd(Long diagnosticId, Long doctorId, String solution, Integer[] drugs) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", diagnosticId);
        updateWrapper.set("doctor_id",doctorId);
        updateWrapper.set("solution",solution);

        Boolean flag = this.update(updateWrapper);

        if (flag == false){
            return Long.valueOf(-1);
        }

        List<DiagnosticDrug> list = new ArrayList<>();

        for (Integer term : drugs){
            DiagnosticDrug diagnosticDrug = new DiagnosticDrug();
            diagnosticDrug.setDiagnosticId(diagnosticId);
            diagnosticDrug.setDrugId(Long.valueOf(term));

            list.add(diagnosticDrug);
        }

        flag = this.diagnosticDrugService.saveBatch(list);

        if (flag == false){
            return Long.valueOf(-2);
        }

        return diagnosticId;
    }

    @Transactional
    public DiagnosticShowResponse show(Long id) {
        DiagnosticShowResponse response = new DiagnosticShowResponse();
        List<DrugTerm> drugs = new ArrayList<>();

        Diagnostic diagnostic = this.getById(id);
        response.setDiagnostic(diagnostic);

        Doctor doctor = this.doctorService.getById(diagnostic.getDoctorId());
        User user = this.userService.getById(diagnostic.getUserId());
        response.setDoctorName(doctor.getName());
        response.setUsername(user.getName());

        QueryWrapper<DiagnosticDrug> queryWrapper = new QueryWrapper();
        queryWrapper.eq("diagnostic_id",diagnostic.getId());
        List<DiagnosticDrug> list = this.diagnosticDrugService.list(queryWrapper);

        for (DiagnosticDrug iterm : list){
            DrugTerm drugTerm = new DrugTerm();

            Drug drug = this.drugService.getById(iterm.getDrugId());

            drugTerm.setName(drug.getName());
            drugTerm.setDrugId(iterm.getDrugId());

            drugs.add(drugTerm);
        }
        response.setDrugTerms(drugs.toArray(new DrugTerm[0]));
        return response;
    }

    public DiagnosticUserListResponse userList(Long userId) {
        DiagnosticUserListResponse response = new DiagnosticUserListResponse();

        Diagnostic diagnostic = this.getById(userId);
        response.setUpdateTime(diagnostic.getUpdatedAt());
        response.setTrouble(diagnostic.getTrouble());
        return response;
    }

    public DiagnosticDoctorListResponse doctorList(Long doctorId) {
        DiagnosticDoctorListResponse response = new DiagnosticDoctorListResponse();

        Diagnostic diagnostic = this.getById(doctorId);
        response.setUpdateTime(diagnostic.getUpdatedAt());
        response.setTrouble(diagnostic.getTrouble());
        return response;
    }

    public List<DiagnosticShowResponse> userHistory(Long id) {
        List<DiagnosticShowResponse> list = new ArrayList<>();
        QueryWrapper<Diagnostic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        List<Diagnostic> diagnosticList = this.list(queryWrapper);
        for(Diagnostic item : diagnosticList){
            list.add(this.show(item.getId()));
        }
        return list;
    }

    public List<DiagnosticShowResponse> doctorHistory(Long id) {
        List<DiagnosticShowResponse> list = new ArrayList<>();
        QueryWrapper<Diagnostic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("doctor_id",id);
        List<Diagnostic> diagnosticList = this.list(queryWrapper);
        for(Diagnostic item : diagnosticList){
            list.add(this.show(item.getId()));
        }
        return list;
    }

    public DiagnosticGetByReservationIdResponse getByReservationId(Long reservationId) {
        DiagnosticGetByReservationIdResponse response = new DiagnosticGetByReservationIdResponse();
        List<DrugTerm> drugs = new ArrayList<>();

        Reservation reservation = this.reservationService.getById(reservationId);

        if (reservation.getDiagnosticId()!=null){
            Diagnostic diagnostic = this.getById(reservation.getDiagnosticId());
            response.setDiagnostic(diagnostic);

            Doctor doctor = this.doctorService.getById(diagnostic.getDoctorId());
            User user = this.userService.getById(diagnostic.getUserId());
            response.setDoctorName(doctor.getName());
            response.setUsername(user.getName());

            QueryWrapper<DiagnosticDrug> queryWrapper = new QueryWrapper();
            queryWrapper.eq("diagnostic_id",diagnostic.getId());
            List<DiagnosticDrug> list = this.diagnosticDrugService.list(queryWrapper);

            for (DiagnosticDrug iterm : list){
                DrugTerm drugTerm = new DrugTerm();

                Drug drug = this.drugService.getById(iterm.getDrugId());

                drugTerm.setName(drug.getName());
                drugTerm.setDrugId(iterm.getDrugId());

                drugs.add(drugTerm);
            }

            response.setDrugTerms(drugs);

            return response;
        }
        return null;
    }
}
